//------------------------------------------------------------------------------
// <copyright file="ItemsCollectionComponent.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System.ComponentModel;
using DotBeer.Business.Entities;
using DotBeer.Data;
using DAnton.Utilities.Extensions;
using DotBeer.Business.Components.Exceptions;

namespace DotBeer.Business.Components
{
    public static class ItemsCollectionComponent
    {
        public static void Save(ItemsCollection itemsColl)
        {
            AbstractDataAccess<ItemsCollection> itemsCollectionDAL = new ItemsCollectionDataAccess();
            HandleExceptions.BusinessLogicPolicy(() => itemsCollectionDAL.Save(itemsColl));
        }

        public static ItemsCollection Load()
        {
            AbstractDataAccess<ItemsCollection> itemsCollectionDAL = new ItemsCollectionDataAccess();
            ItemsCollection itemsCollection = null;

            HandleExceptions.BusinessLogicPolicy(() =>
                {
                    itemsCollection = itemsCollectionDAL.Load();
                });

            if (itemsCollection == null)
                itemsCollection = new ItemsCollection();

            itemsCollection.Items.Sort(i => i.Name, ListSortDirection.Ascending);
            return itemsCollection;
        }
    }
}
